<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card title="学员管理">
                    <Row @keydown.enter.native="handleSearch">
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
                            <Form-item label="学员姓名" prop="userName">
                                <Input type="text" v-model="searchForm.userName" placeholder="请输入学员姓名" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="手机号" prop="mobile">
                                <Input type="text" v-model="searchForm.mobile" placeholder="请输入手机号" clearable style="width: 200px"/>
                            </Form-item>
                            <span v-if="drop">
                                <Form-item label="创建时间">
                                  <DatePicker v-model="selectDate" type="daterange" format="yyyy-MM-dd" clearable @on-change="selectDateRange" placeholder="选择起始时间" style="width: 200px"></DatePicker>
                                </Form-item>
                            </span>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                                <a class="drop-down" @click="dropDown">{{dropDownContent}}
                                    <Icon :type="dropDownIcon"></Icon>
                                </a>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="delAll" icon="md-trash">批量删除</Button>
                        <Button @click="giveScore" icon="md-paper">赠送积分</Button>
                        <Button @click="giveIntegral" icon="md-paper">赠送学习币</Button>
                        <Button @click="getDataList" icon="md-refresh">刷新</Button>
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                    </Row>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <student-drawer ref="studentDrawer"></student-drawer>
        <score-drawer ref="scoreDrawer"></score-drawer>
        <integral-drawer ref="integralDrawer"></integral-drawer>
        <record-drawer ref="recordDrawer"></record-drawer>
        <vip-drawer ref="vipDrawer"></vip-drawer>
        <userScore-drawer ref="userScoreDrawer"></userScore-drawer>
        <amountRecord-drawer ref="amountRecordDrawer"></amountRecord-drawer>
    </div>
</template>

<script>
    import {
        studentList, setStatus, delStudent
    } from "@/api/bus";
    import circleLoading from "../../my-components/circle-loading.vue";
    import eduDrawer from "./eduDrawer";
    import workDrawer from "./workDrawer";
    import studentDrawer from "./student-drawer";
    import showStudent from "../company-manage/studentDrawer";
    import scoreDrawer from "./score-drawer";
    import integralDrawer from "./integral-drawer";
    import recordDrawer from "./record-drawer";
    import vipDrawer from "./vip-drawer";
    import userScoreDrawer from "./userScore-drawer";
    import amountRecordDrawer from "./amountRecord-drawer";

    export default {
        name: "student-manage",
        components: {
            circleLoading,
            studentDrawer,
            showStudent,
            scoreDrawer,
            integralDrawer,
            recordDrawer,
            vipDrawer,
            userScoreDrawer,
            amountRecordDrawer
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "createTime", // 默认排序字段
                    order: "desc", // 默认排序方式
                    userName: "",
                    userType: "UT_STUDENT",
                    mobile: "",
                    startDate: "",
                    endDate: ""
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        type: "index",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "学员姓名",
                        key: "userName",
                        width: 130,
                        sortable: true
                    },   {
                        title: "所属企业",
                        key: "compName",
                        width: 120,
                        align: "center"
                    },{
                        title: "头像",
                        key: "avatar",
                        width: 80,
                        align: "center",
                        render: (h, params) => {
                            return h("Avatar", {
                                props: {
                                    src: params.row.avatar
                                }
                            });
                        }
                    },
                    {
                        title: "手机",
                        key: "mobile",
                        width: 115,
                        sortable: true
                    },
                    {
                        title: "邮箱",
                        key: "email",
                        width: 160,
                        sortable: true
                    },
                    {
                        title: "性别",
                        key: "sex",
                        width: 70,
                        align: "center",
                        render: (h, params) => {
                            let re = "";
                            if (params.row.sex === 1) {
                                re = "男";
                            } else if (params.row.sex === 2) {
                                re = "女";
                            }
                            return h("div", re);
                        }
                    },
                    {
                        title: "状态",
                        key: "status",
                        align: "center",
                        width: 120,
                        render: (h, params) => {
                            return h(
                                "i-switch",
                                {
                                    props: {
                                        value: params.row.status,
                                        size: "large",
                                        "true-value": 1,
                                        "false-value": 0
                                    },
                                    on: {
                                        "on-change": v => {
                                            this.setStatus(params.row, v);
                                        }
                                    }
                                },
                                [
                                    h("span", {slot: "open"}, "是"),
                                    h("span", {slot: "close"}, "否")
                                ]
                            );
                        }
                    },
                    {
                        title: "年龄",
                        key: "age",
                        width: 80,
                        sortable: true
                    },
                    {
                        title: "学习币余额",
                        key: "amount",
                        width: 120,
                        sortable: true
                    },
                    {
                        title: "积分余额",
                        key: "integral",
                        width: 120,
                        sortable: true
                    },
                    {
                        title: "会员购买日期",
                        key: "vipBugTime",
                        sortable: true,
                        sortType: "desc",
                        width: 160
                    }, {
                        title: "会员截止日期",
                        key: "expiryDate",
                        sortable: true,
                        sortType: "desc",
                        width: 160
                    }, {
                        title: "注册日期",
                        key: "createTime",
                        sortable: true,
                        sortType: "desc",
                        width: 160
                    },{
                        title: "操作",
                        key: "action",
                        width: 320,
                        align: "center",
                        fixed: "right",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            size: "small",
                                            type: "info"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ), h(
                                    eduDrawer,
                                    {
                                        props: {
                                            id: params.row.id
                                        },
                                        style: {
                                            marginRight: "5px"
                                        }
                                    }
                                ), h(
                                    workDrawer,
                                    {
                                        props: {
                                            id: params.row.id
                                        },
                                        style: {
                                            marginRight: "5px"
                                        }
                                    }
                                ),
                                h(
                                    "Dropdown",
                                    {
                                        props: { transfer: true },
                                        on: {
                                            "on-click": v => {
                                                this.changeDropDown(params.row, v);
                                            }
                                        }
                                    },
                                    [
                                        h(
                                            "Button",
                                            {
                                                props: { size: "small" },
                                                style: {
                                                    height: "23.5px"
                                                }
                                            },
                                            "更多操作 ∨"
                                        ),

                                        h("DropdownMenu", { slot: "list" }, [
                                            h("DropdownItem", { props: { name: "score" } }, "积分记录"),
                                            h("DropdownItem", { props: { name: "amount" } }, "学习币记录"),
                                            h("DropdownItem", { props: { name: "ability" } }, "测评分数"),
                                            h("DropdownItem", { props: { name: "vip" } }, "添加VIP"),
                                            h("DropdownItem", { props: { name: "delete" } }, "删除")
                                        ])
                                    ]
                                )
                            ]);


                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {
                this.getDataList();
            },
            getDataList() {
                // 多条件搜索用户列表
                this.loading = true;
                studentList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.username = "";
                this.mobile = "";
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            setStatus(row, v) {
                let params = {
                    id: row.id,
                    status: v
                }
                setStatus(params).then(res => {
                    if (res.status === true) {
                        this.$Message.success(res.info);
                    }
                });
                ;
            },
            edit(row) {
                let query = {type: 1, id: row.id};
                this.$refs.studentDrawer.showDrawer(query);
            },
            getScoreRecord(row) {
                let query = {id: row.id};
                this.$refs.recordDrawer.showDrawer(query);
            },
            getAmountRecord(row) {
                let query = {id: row.id};
                this.$refs.amountRecordDrawer.showDrawer(query);
            },
            getAbility(row) {
                let query = {id: row.id};
                this.$refs.userScoreDrawer.showDrawer(query);
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除用户 " + v.userName + " ?",
                    onOk: () => {
                        delStudent(v.id).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.getDataList();
                            }
                        });
                    }
                });
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            showSelect(e) {
                this.exportData = e;
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        delStudent(ids).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getDataList();
                            }
                        });
                    }
                });
            },
            changeDropDown(row, v) {
                if(v == "delete"){
                    this.remove(row);
                }
                if(v == "score"){
                    this.getScoreRecord(row);
                }
                if(v == "vip"){
                    this.giveVip(row);
                }
                if(v == "amount"){
                    this.getAmountRecord(row);
                }
                if(v == "ability"){
                    this.getAbility(row);
                }
            },
            giveScore() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择学员");
                    return;
                }
                let ids = "";
                this.selectList.forEach(function (e) {
                    ids += e.id + ",";
                });
                ids = ids.substring(0, ids.length - 1);
                let query = {ids: ids};
                this.$refs.scoreDrawer.showDrawer(query);
            },
            giveIntegral() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择学员");
                    return;
                }
                let ids = "";
                this.selectList.forEach(function (e) {
                    ids += e.id + ",";
                });
                ids = ids.substring(0, ids.length - 1);
                let query = {ids: ids};
                this.$refs.integralDrawer.showDrawer(query);
            },
            giveVip(row) {
                let query = {id: row.id};
                this.$refs.vipDrawer.showDrawer(query);
            }
        },
        mounted() {
            this.init();
        }
    }
</script>
